# Python 趣味编程：第 9 章　
# 练习问题 9.3 利用随机数生成地雷
# --------------------------
# 程序名: ex09-mine-list-1.py

from tkinter import Tk, Canvas, CENTER
from dataclasses import dataclass, field
import math
import random

# 初始设定值
OFFSET_X = 100
OFFSET_Y = 100
CELL_SIZE = 40
FONT_SIZE = 20
CELL_CENTER = CELL_SIZE / 2
BOARD_WIDTH = 3
BOARD_HEIGHT = 3
FONT = "Helvetica " + str(FONT_SIZE)

# =================================================
# 管理整个游戏的棋局
@dataclass
class Board:
    width: int
    height: int
    mine: list = field(init=False)
    is_open: list = field(init=False)

    def __post_init__(self):
        self.mine = self.false_table()
        self.is_open = self.false_table()
 
    def false_table(self):
        cells = [[False for y in range(self.height)]
                        for x in range(self.width)]
        return cells

    # 设置地雷
    def set_mine(self, num):
        num_mine = 0
        while num_mine < num:  # 按指定数量制造地雷
            x = random.randrange(self.width)   # 横方向
            y = random.randrange(self.height)  # 縦方向
            if not self.mine[x][y]:  # 如果地雷还没有被设置的话
                self.mine[x][y] = True  # 在这里设置地雷
                num_mine += 1  # 将已经生成的地雷数增加1
                print("mine =", x, ",", y)     # 开发时的确认用

tk = Tk()
canvas = Canvas(tk, width=500, height=400, bd=0)
canvas.pack()

board = Board(BOARD_WIDTH, BOARD_HEIGHT)
board.set_mine(2)
board.is_open[0][0] = True
board.is_open[1][2] = True
print(board.mine)
print(board.is_open)
